package org.lucky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lucky.mapper.ShareUrlMapper;
import org.lucky.service.IShareUrlService;
import org.lucky_common.domain.ShareUrl;
import org.lucky_common.util.AjaxResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ShareUrlServiceImpl extends ServiceImpl<ShareUrlMapper, ShareUrl> implements IShareUrlService {

    @Resource
    private ShareUrlMapper shareUrlMapper;

    @Value("${default.url}")
    private String url;

    @Override
    public AjaxResult getUrl() {
        List<ShareUrl> shareUrls = shareUrlMapper.selectList(new QueryWrapper<ShareUrl>().eq("status",1));
        if (shareUrls != null){
            Map<Boolean, List<ShareUrl>> groupedData = shareUrls.stream()
                    .collect(Collectors.groupingBy(ShareUrl::isRandom));
            return AjaxResult.success(groupedData);
        }else {
            return AjaxResult.error(url);
        }
    }
}
